package com.heitian.ssm.service.impl;

import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.controller.UserController;
import com.heitian.ssm.dao.UserEntityMapper;
import com.heitian.ssm.model.UserEntity;
import com.heitian.ssm.service.UserLoginService;
import com.heitian.ssm.service.UserService;
import com.heitian.ssm.utils.FastJsonUtils;
import com.heitian.ssm.utils.MD5;
import com.heitian.ssm.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-22
 * Time: 01:01
 * Des:
 */
@Service("userLoginService")
@Transactional(rollbackFor = Exception.class)
public class UserLoginServiceImpl implements UserLoginService {
    private Logger log = Logger.getLogger(UserLoginServiceImpl.class);
    @Autowired
    private UserEntityMapper userEntityMapper;

    public UserEntity login(String name,String passwd) {
        log.info("入参姓名"+name);
        if(StringUtils.isBlank(name) || StringUtils.isBlank(passwd)){
            return null;
        }
        String passedSalt=null;
        try {
            passedSalt= MD5.md5(passwd, WebConstants.SALT);
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
        }
        UserEntity userEntity=new UserEntity();
        userEntity.setUserName(name);
        userEntity.setUserPwd(passedSalt);
        UserEntity userEntity1= userEntityMapper.selectForLogin(userEntity);
        log.info("出参信息"+ FastJsonUtils.toJSONString(userEntity1));
        return userEntity1;
    }

    /**
     * 注册
     * @param userEntity
     * @return
     */
    public int register(UserEntity userEntity) {
        if(userEntity == null){
            return 0;
        }
        return userEntityMapper.insert(userEntity);
    }

    public UserEntity getUserCard(String userName) {
        log.info("入参userName:"+userName);
        List<UserEntity> userEntityList=userEntityMapper.selectUserCardByName(userName);
        log.info("出参userEntituList:"+FastJsonUtils.toJSONString(userEntityList));
        if(userEntityList != null && userEntityList.size() > 0){
            return userEntityList.get(0);
        }
        return null;
    }
}
